package org.example.securevideotransmission.util;

import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;

import java.io.File;
import java.util.function.Function;

public class VideoUtil {
    public static void processFrames(File input, File output, Function<byte[], byte[]> processor) throws Exception {
        try (FrameGrabber grabber = FrameGrabber.createDefault(input);
             FrameRecorder recorder = FrameRecorder.createDefault(output, grabber.getImageWidth(), grabber.getImageHeight())) {

            grabber.start();
            recorder.start();

            Frame frame;
            while ((frame = grabber.grabFrame()) != null) {
                byte[] processedData = processor.apply((byte[]) frame.image[0].array());
                recorder.record(new Frame());
            }

            grabber.stop();
            recorder.stop();
        }
    }
}
